<template>
  <div class="app-container">
    <!-- 搜索区 -->
    <el-row>
      <el-form v-hasPermi ref="queryForm" :model="queryParams" size="small" :inline="true">
        <el-form-item label="字典名称" prop="dictName">
          <el-input
            placeholder="请输入内容"
            v-model="queryParams.dictName"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input
            placeholder="请输入内容"
            v-model="queryParams.dictType"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item label="字典状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="状态"
            clearable>
            <el-option
              v-for="dict in statusList"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="timeData"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!--功能区-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate()"
          :disabled="single"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete()"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
        >导出</el-button>
      </el-col>
    </el-row>
    <!-- 数据展示区table-->
    <el-table v-loading="loading" :data="pageData.rows" @selection-change="handleSelectionChange">
      <el-table-column type="selection"/>
      <el-table-column label="字典编号" prop="id" width="100" />
      <el-table-column label="字典名称" prop="dictName" />
      <el-table-column label="字典类型">
        <template slot-scope="scope">
          <el-link type="primary" >
            <router-link :to="{path:'/system/dictData',query:{dictType:scope.row.dictType}}">{{scope.row.dictType}}
            </router-link>
          </el-link>

        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==='0'">正常</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark"  />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页条 -->
    <div style="margin-top: 10px;">
      <el-pagination
        v-if="pageData.total>0"
        :current-page="queryParams.page"
        :page-sizes="[10, 20, 30, 40,50,100]"
        :page-size="queryParams.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 新增和修改对话框-->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="字典名称" prop="dictName">
              <el-input v-model="form.dictName" placeholder="请输入字典名称" />
            </el-form-item>

            <el-form-item label="字典类型" prop="dictType">
                  <el-input v-model="form.dictType" placeholder="请输入字典类型" />
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusList"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>

          <el-form-item label="请输入内容">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.remark">
            </el-input>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loading1" @click="submitForm">确 定</el-button>
        <el-button @click="cancelForm">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import pageMixin from '/src/utils/mixin/psgeMixin.js'
import addorupdateMixin from '/src/utils/mixin/addorupdateMixin.js'
import {getlist, findType, addORupdateType,deleteType} from '/src/api/system/dictType'
import {getDictTypeList} from "../../../api/system/dictData";
export default {
  mixins: [pageMixin, addorupdateMixin],
  data() {
    return {
      showDict: true,
      timeData: [],
      statusList: [],
      // 确定按钮加载，防止多次点击
      loading1: false,
      rules: {
        dictName: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
        ],
        dictType: [
          { required: true, message: '类型不能为空', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    // 跟据id删除
    beforehandleDelete(id) {
      deleteType(id).then(resp => {
        this.fetchData()
      })
    },
    // 修改之前查询
    beforeHandlerUpdate(id) {
      findType({id:id}).then(resp => {
        this.form=resp
      })
    },
    // 新增或修改
    afterSubmitFormHandler() {
    this.loading1=true
      addORupdateType(this.form).then(resp => {
        this.fetchData()
        this.loading1=false
        this.open=false
      })
    },
    // 查字典类型
    fetchData() {
      this.loading = true
      // 删除 日期
      delete this.queryParams.beginTime
      delete this.queryParams.endTime
      if (this.timeData && this.timeData.length === 2) {
        this.queryParams.startTime = this.timeData[0]
        this.queryParams.endTime = this.timeData[1]
      }
      getlist(this.queryParams).then(resp => {
        this.pageData = resp
        this.loading = false
      })
    },
    // 状态数据
    fetchCommonData() {
      getDictTypeList('sys_normal_disable').then(resp=> {
        this.statusList= resp
      })
    },
    // 搜索
    handleQuery() {
      this.queryParams.page = 1
      this.fetchData()
    },
    // 查询重置
    resetQuery() {
      this.$refs['queryForm'].resetFields()
      this.fetchData()
      this.disa1 = false
      this.disa2 = false
    }
  },
  created() {
    this.fetchCommonData()
  }

};
</script>
